---@class UIStoryReview : UIBase
UIStoryReview = UIBase.New(PanelResNames.UIStoryReview)
local t = UIStoryReview

function t.Init(func)
    UIBase.Init(t, func)
end

function t:OnCreate()
    --剧情查看
    TAManager.TrackByDictionary("story_review")
    self.btnBack = self:GetChild("btnBack")
    self.bgClose = self:GetChild("bgClose")
    self.btnHelp = self:GetChild("help")
    self.btnType = self:GetChild("btnType")
    self.btnHard = self:GetChild("btnHard")
    self.btnNormal = self:GetChild("btnNormal")
    self.itemList = self:GetList("listStory")
    self.listCtrl = self:GetController("ifShowList")
    self.storyModeOpen = self:GetController("storyModeOpen")
    self.storyMode = self:GetController("storyMode")
    self.detailList = self:GetChild("UIStoryReviewList/listStory")
    self.numChapter = self:GetChild("UIStoryReviewList/numChapter")
    self.detailTitle = self:GetChild("UIStoryReviewList/titleChapter")
    self.storyReviewCtrl =  self:GetChild("UIStoryReviewList"):GetController("storyMode")
    
    self:SetDynamicList(self.itemList,self.ItemRender)
    self:RegisterOnClick(self.btnBack,self.ClickClose)
    self:RegisterOnClick(self.bgClose,self.ClickClose)
    self:RegisterOnClick(self.btnHelp,self.ClickHelp)
    self:RegisterOnClick(self.btnType,self.ClickType)
    self:RegisterOnClick(self.btnNormal,self.ClickNormal)
    self:RegisterOnClick(self.btnHard,self.ClickHard)
    self:RegisterOnClick(self:GetChild("UIStoryReviewList/bgClose"),function()
        self.listCtrl.selectedIndex = 0
    end)
    self.storyMode.selectedIndex = 0
    self:SetDynamicList(self.detailList,self.DetailRender)
    self.poolMgr = ComponentPoolMgr.New(t.ChapterItem)
    self.btnNormal.text = getLanguage("TeamsStoryModeTips2")
    self.btnHard.text = getLanguage("TeamsStoryModeTips3")
    self.ishard = false
end

function t:OnEnable()
    self:Refresh()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.StoryModel) then
        self.storyModeOpen.selectedIndex = 1
    end
    if UIBottom.lastSelectedIndex == 2 and UIBattle.controllerStoryMode.selectedIndex == 1 then
        self:ClickHard()
    end
end
function t:ClickNormal()
    self.storyMode.selectedIndex = 0
    self.ishard = false
    self:Refresh()
end
function t:ClickHard()
    self.storyMode.selectedIndex = 1
    self.ishard = true
    self:Refresh()
end
function t:OnDisable()
end
function t:OnDestroy()
    if self.poolMgr then
        self.poolMgr:OnDestroy()
    end
    self.poolMgr = nil
end
function t:DetailRender(index,obj)
    local cfg = self.dataList[logicMgr.StoryMgr.storyIndex][index]
    local title = obj:GetChild("titleSection")
    local titleIndex = obj:GetChild("numSection")
    local ctrlLock = obj:GetController("lockState")
    titleIndex.text = cfg.f_ChapterID .."-"..(cfg.f_StoryID % 10)
    title.text = "";
    local nameList = string.split(getLanguage(cfg.f_Name), ".");
    if #nameList > 1 then
        title.text = nameList[2];
        for i = 3, #nameList do
            title.text = title.text.. "." .. nameList[i];
        end
    else
        title.text = getLanguage(cfg.f_Name);
    end
    if index == 1 then
        self.numChapter.text = "第"..logicMgr.StoryMgr.storyIndex.."章"
        self.detailTitle.text = title.text
    end
    local nowMapId = logicMgr.MapManager.getMapId();
    if self.ishard then
        nowMapId = logicMgr.StoryHardManager.getDungeonId();
    end
    if nowMapId < cfg.f_mapid then
        ctrlLock.selectedIndex = 1
    else
        ctrlLock.selectedIndex = 0
    end
    if self.ishard then
        obj:GetController("storyMode").selectedIndex = 1
    else
        obj:GetController("storyMode").selectedIndex = 0
    end
    obj.onClick:Clear()
    obj.onClick:Add(function()
        if ctrlLock.selectedIndex == 0 then
            --剧情选择
            TAManager.TrackByDictionary("story_choose",{story_id = tostring(cfg.f_StoryID)})
            logicMgr.StoryMgr.ShowStoryById(cfg.f_StoryID,EnumConst.StoryShowType.review)
        else
            local stageCfg = logicMgr.MapManager.GetMapcfgByID(cfg.f_mapid)
            if self.ishard then
                stageCfg = logicMgr.StoryHardManager.getDungeonTableDataById(cfg.f_mapid);
            end
            if not stageCfg then
                if self.ishard then
                    logError("找不到t_stagehardmode  id:"..cfg.f_mapid)
                else
                    logError("找不到t_stage  id:"..cfg.f_mapid)
                end
            end
            local str = stageCfg.f_ChapterId .. "-" .. stageCfg.f_StageNum;
            UISysTips.AddMsg(string.format(getLanguage("FunctionTipsStage"), str));
        end
    end)
end


function t:Refresh()
    local cfgs=logicMgr.StoryMgr.getStoryTableDataByStageId(nil, self.ishard );
    self.dataList = {}
    for k,v in pairs(cfgs) do
        if v.f_ChapterID <= G.dataTable["t_global"][tostring(307)].f_int_value then
            if not self.dataList[v.f_ChapterID] then
                self.dataList[v.f_ChapterID] = {}
            end
            table.insert(self.dataList[v.f_ChapterID],v)
        end
    end
    for _,v in pairs(self.dataList) do
        table.sort(v,function(a, b)
            return  a.f_StoryID < b.f_StoryID
        end)
    end
  
    self.itemList.numItems = #self.dataList
    self.btnType.selected = GetPlayerPrefs("StoryBother") == "1"
end

function t:ItemRender(index,go)
    local data = self.dataList[index]
    local item = self.poolMgr:GetBindScript(go)
    item:Refresh(data,self.listCtrl,self.detailList,index,self.storyReviewCtrl,self.ishard)
    if self.ishard then
        go:GetController("storyMode").selectedIndex = 1
    else
        go:GetController("storyMode").selectedIndex = 0
    end
end
function t:ClickClose()
    closeUI(PanelResNames.UIStoryReview)
end
function t:ClickHelp()
    logicMgr.UIShowManager.ShowCommonTips("HelpTitle33","HelpDes33", "FunctionStory2080")
end
function t:ClickType()
    SetPlayerPrefs("StoryBother",self.btnType.selected and 1 or 0)
end

---@class UIStoryChapterItem :UIComponentInterface
t.ChapterItem = Class("ChapterItem",UIComponentInterface)
function t.ChapterItem:OnCreate()
    self.ctrlLock = self:GetController("lock")
    self.titleChapter = self:GetChild("titleChapter")
    self.titleChapterName = self:GetChild("titleChapterName")
    self.titleSection = self:GetChild("titleSection")
    self.btnPlay = self:GetChild("btnPlay")
    self.reward = self:GetChild("showReward")
    self:RegisterOnClick(self.btnPlay,self.ClickPlay)
end
function t.ChapterItem:OnDestroy()
    self.data = nil
    self.detailCtrl = nil
    self.list = nil
    self.index = nil
end
function t.ChapterItem:Refresh(data,ctrl,list,index,viewctrl,ishard)
    self.data = data
    self.detailCtrl= ctrl
    self.list = list
    self.index = index
    local nameList = string.split(getLanguage(self.data[1].f_Name), ".");
    if #nameList > 1 then
        self.titleChapterName.text = nameList[2];
        for i = 3, #nameList do
            self.titleChapterName.text = self.titleChapterName.text.. "." .. nameList[i];
        end
    else
        self.titleChapterName.text = getLanguage(self.data[1].f_Name);
    end
    self.titleSection.text = ""
    local nowMapId = logicMgr.MapManager.getMapId();
    if ishard then
        nowMapId = logicMgr.StoryHardManager.getDungeonId();
    end
    if nowMapId < data[1].f_mapid then
        self.ctrlLock.selectedIndex = 1
    else
        self.ctrlLock.selectedIndex = 0
    end
    if ishard then
        viewctrl.selectedIndex = 1
    else
        viewctrl.selectedIndex = 0
    end
    self.titleChapter.text = "第"..self.index.."章"
    self.fguiObj.onClick:Clear()
    self.fguiObj.onClick:Add(t.ChapterItem.ClickItem,self)
    self:RefreshReward()
end
function t.ChapterItem:ClickItem()
    if self.ctrlLock.selectedIndex == 1 then
        local stageCfg = dataTable.getDataTableByStringId("t_stage",self.data[1].f_mapid)
        if not stageCfg then
            logError("找不到t_stage  id:"..self.data[1].f_mapid)
            return
        end
        local str = stageCfg.f_ChapterId .. "-" .. stageCfg.f_StageNum;
        UISysTips.AddMsg(string.format(getLanguage("FunctionTipsStage"), str));
    end
end
function t.ChapterItem:RefreshReward()
    if string.IsNilOrEmpty(self.data.f_Reward) then
        self.reward.visible = false
    else
        self:SetSimpleItem(self.reward,self.data.f_Reward[1][1],self.data.f_Reward[1][2])
    end
end
function t.ChapterItem:ClickPlay()
    table.sort(self.data,function(a,b)
        return a.f_StoryID < b.f_StoryID
    end)
    self.detailCtrl.selectedIndex = 1
    logicMgr.StoryMgr.SetIndex(self.index)
    self.list.numItems = #self.data
end

return t